<?php
class FileToSQL {

	private $lines;
	private $TableName;
	private $Database;
	private $HasInnerKey = false;
	public function __construct($filename){
		ini_set("memory_limit","800M");
		$this->lines = file($filename);
	}
	public function __destruct(){
		$lines = array();		
	}
	public function SetSQLTableName($name){
		$this->TableName = $name;
	}
	public function SetSQLDatabase($name){
		$this->Database = $name;
	}
	public function UseInnerKey()
	{
		$this->HasInnerKey = true;
	}
	public function IgnoreInnerKey()
	{
		$query = "";
		if ( $this->HasInnerKey==true )
		{
			$query = "select * from ".$this->TableName.";";
			require_once("./includes/class.MySQL.inc.php");
			// $sql = new MySQL("localhost", "szofttech", "alma");
			$sql = new MySQL("localhost", "szofttech", "alma");				//$sql->ConnectionTest();
			$sql->select_db($this->Database);
			$resource = $sql->query($query);
			$query="(";
			for ($i = 0; $i < $this->NumFieldsinTable(); $i++)
			{
				
				$fieldname = $sql->FieldName($i);				
				if ( $fieldname!='id')
				{
					$query.= '`';
					$query.= $fieldname;
					$query.= '`';
					if ($i<$this->NumFieldsinTable()-1 ) $query.= ', '; 
				}				
			}
			
			$query.=")";
			
			// echo $query;
		}
		return $query;
	}
	public function NumFieldsinTable()
	{
		$query = "select * from ".$this->TableName.";";
		require_once("./includes/class.MySQL.inc.php");
		$sql = new MySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db($this->Database);
		$result = $sql->query($query);
		return mysql_num_fields($this->Query($query));
	}
	public function ClearTable()
	{
		$query = 'TRUNCATE TABLE `'.$this->TableName.'`';
		require_once("./includes/class.MySQL.inc.php");
		$sql = new MySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db($this->Database);
		$result = $sql->query($query);
		return $result;
	}
	public function MakeInsertQuery()
	{
		$i=0;
		$maxquerynum = 100000;
		$NumFields = $this->NumFieldsinTable();
		// Loop through our array, show HTML source as HTML source; and line numbers too.
		$querynum = 0;
		foreach ($this->lines as $line_num => $line) {
			$query="";
			if ($i != 0)
			{
				$fields = explode(';', $line);
				$query .= "INSERT INTO ".$this->TableName." ".$this->IgnoreInnerKey()." VALUES (";
				$f = 0;
				foreach($fields as $field)
				{
					if($f != 0) $query .= ", ";
					if ($field)
					{
						if ( !is_numeric($field) || $f==0 )
						{
							$field = str_replace('"', '', $field);
							$field = str_replace("'", "\'", $field);
							$field = str_replace(",","\,", $field);
							$query .= "'";
							$query .= $field;
							$query .= "'";				
						}
						else
						{
							$query .= $field;
						}
					}
					else
					{
						$query .= "NULL";
					}
					$f++;
					
				}
				if ( !$this->HasInnerKey )
				{
					$RequiredFiledToFill = $NumFields - $f;
					for ($k = 0; $k<$RequiredFiledToFill; $k++)
					{
						$query .= ",0";
					}
				}
				$query .= ");";
				// echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
				// echo $query."<br>";
				$querynum++;
				// if ($querynum == $maxquerynum)
				// {
					// echo "--Megy SQLbe--<br>";
					$this->Query($query);
					// $querynum=0;
					// $query = "";
				// }
				
			}
			$i++;
			
		}

		$query .= "COMMIT;";
		return $query;
	}
	public function Query($query){
		require_once("./includes/class.MySQL.inc.php");
		$sql = new MySQL("localhost", "szofttech", "alma");
		//$sql->ConnectionTest();
		$sql->select_db($this->Database);
		$result = $sql->query($query);
		return $result;		
	}
	public function Process()
	{
		$this->ClearTable();
		$query = $this->MakeInsertQuery();
		// return $this->Query($query);
	}
}
?>